package com.example.riceexambackend.controller;

import com.example.riceexambackend.common.errorcode.ControllerErrorCodeConstants;
import com.example.riceexambackend.common.exception.ControllerException;
import com.example.riceexambackend.common.pojo.CommonResult;
import com.example.riceexambackend.common.utils.JacksonUtil;
import com.example.riceexambackend.controller.param.question.QuestionAddParam;
import com.example.riceexambackend.controller.result.question.QuestionAddResult;
import com.example.riceexambackend.controller.result.question.QuestionSelectResult;
import com.example.riceexambackend.service.QuestionService;
import com.example.riceexambackend.service.dto.question.QuestionAddDTO;
import com.example.riceexambackend.service.dto.question.QuestionSelectDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Resource
    private QuestionService questionService;
    private static final Logger logger = LoggerFactory.getLogger(QuestionController.class);

    @GetMapping("/select")
    public CommonResult<List<QuestionSelectResult>> selectAllQuestion() {
        // 日志打印
        logger.info("QuestionController selectAllQuestion");
        // 调用查询全部题目业务逻辑
        List<QuestionSelectDTO> questionSelectDTOList = questionService.selectAllQuestion();
        List<QuestionSelectResult> resultList = convertToQuestionSelectResultList(questionSelectDTOList);
        return CommonResult.success(resultList);
    }

    private List<QuestionSelectResult> convertToQuestionSelectResultList(List<QuestionSelectDTO> questionSelectDTOList) {
        if (questionSelectDTOList == null) {
            throw new ControllerException(ControllerErrorCodeConstants.QUESTION_SELECT_ALL_ERROR);
        }
        List<QuestionSelectResult> questionSelectResultList = new ArrayList<>();
        for (QuestionSelectDTO questionSelectDTO : questionSelectDTOList) {
            QuestionSelectResult questionSelectResult = new QuestionSelectResult();
            questionSelectResult.setId(questionSelectDTO.getId());
            questionSelectResult.setQuestionName(questionSelectDTO.getQuestionName());
            questionSelectResult.setOptionA(questionSelectDTO.getOptionA());
            questionSelectResult.setOptionB(questionSelectDTO.getOptionB());
            questionSelectResult.setOptionC(questionSelectDTO.getOptionC());
            questionSelectResult.setOptionD(questionSelectDTO.getOptionD());
            questionSelectResult.setRightAnswer(questionSelectDTO.getRightAnswer());
            questionSelectResult.setUploadUser(questionSelectDTO.getUploadUser());
            questionSelectResult.setQuestionType(questionSelectDTO.getQuestionType());
            questionSelectResult.setQuestionStatus(questionSelectDTO.getQuestionStatus());
            questionSelectResultList.add(questionSelectResult);
        }
        return questionSelectResultList;
    }

    @PostMapping("/add")
    public CommonResult<QuestionAddResult> addQuestion(@Validated @RequestBody QuestionAddParam param) {
        // 日志打印
        logger.info("QuestionController addQuestion:{}", JacksonUtil.writeValueAsString(param));
        // 调用新增题目业务逻辑
        QuestionAddDTO questionAddDTO = questionService.insertQuestion(param);
        QuestionAddResult result = convertToQuestionAddResult(questionAddDTO);
        return CommonResult.success(result);
    }

    private QuestionAddResult convertToQuestionAddResult(QuestionAddDTO questionAddDTO) {
        if (questionAddDTO == null ||
            questionAddDTO.getQuestionId() <= 0) {
            throw new ControllerException(ControllerErrorCodeConstants.QUESTION_ADD_ERROR);
        }
        QuestionAddResult questionAddResult = new QuestionAddResult();
        questionAddResult.setQuestionId(questionAddDTO.getQuestionId());
        return questionAddResult;
    }
}
